package qtype

type QObject struct {
	QBase
	Keys   []string
	Values []interface{}
}

func NewQObject() *QObject {
	this := &QObject{}
	// key type: int, value type: *int
	//Mp := rbtree.NewMap(int64(0), new(interface{}), func(a, b interface{}) int {
	//	return a.(int) - b.(int)
	//})
	//var a int64 = 1
	//Mp.Insert(a, &a)
	// iterator
	//for it, i := Mp.Begin(), 0; it != Mp.End(); it = it.Next() {
	//	fmt.Println(it.GetKey(), it.GetVal())
	//	i++
	//}
	return this
}

func (this *QObject) Put(key string, value interface{}) {
	i := this.IndexOf(key)
	if i > -1 {
		this.Keys[i] = key
		this.Values[i] = value
	} else {
		this.Keys = append(this.Keys, key)
		this.Values = append(this.Values, value)
	}
}

/**
后续使用二分搜索
*/
func (this *QObject) IndexOf(key string) int {
	if this.Keys == nil {
		return -1
	}
	for i := range this.Keys {
		if this.Keys[i] == key {
			return i
		}
	}
	return -1
}

func (this *QObject) Get(key string) interface{} {
	i := this.IndexOf(key)
	if i == -1 {
		return nil
	}
	return this.Values[i]
}

func (this *QObject) Delete(key string) {
	i := this.IndexOf(key)
	if i == -1 {
		return
	}
	this.Keys = append(this.Keys[:i], this.Keys[i+1:]...)
	this.Values = append(this.Values[:i], this.Values[i+1:]...)
}
